package at.ac.univie.zsu.aguataplan.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import at.ac.univie.zsu.aguataplan.domain.Club;
import at.ac.univie.zsu.aguataplan.domain.Game;
import at.ac.univie.zsu.aguataplan.domain.Team;
import at.ac.univie.zsu.aguataplan.domain.Video;

/**
 * @author gerry
 * 
 */
@Repository
public class GameDaoBean implements GameDao {

	@PersistenceContext
	transient EntityManager em;

	@Autowired
	private Dao dao;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public GameDaoBean() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	@Transactional
	public void cleanGame() throws Exception {
		List<Game> games = dao.findAll(Game.class);
		List<Team> teams = dao.findAll(Team.class);
		List<Club> clubs = dao.findAll(Club.class);
		List<Video> videos = dao.findAll(Video.class);

		for (Video video : videos) {
			em.remove(video);
		}

		for (Game game : games) {
			em.remove(game);
		}

		for (Team team : teams) {
			em.remove(team);
		}

		for (Club club : clubs) {
			em.remove(club);
		}
	}

	// --------------

	@Override
	public Game addGame(Game game) throws Exception {
		dao.persist(game);
		return game;
	}

	@Override
	public void deleteGame(long id) throws Exception {
		dao.remove(Game.class, id);
	}

	@Override
	@Transactional
	public Game getGame(long id) throws Exception {
		Game game = dao.find(Game.class, id);

		return game;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Game> getGameList() throws Exception {
		List<Game> games;
		games = em.createQuery("SELECT o FROM " + Game.class.getName() + " o ORDER BY description").getResultList();

		return games;
	}

	@Override
	public Game mergeGame(Game game) throws Exception {
		Game retGame = dao.merge(game);
		return retGame;
	}

	// --------------

	@Override
	public Video addVideo(Video video) throws Exception {
		dao.persist(video);
		return video;
	}

	@Override
	public void deleteVideo(long id) throws Exception {
		dao.remove(Video.class, id);

	}

	@Override
	public Video getVideo(long id) throws Exception {
		Video video = dao.find(Video.class, id);

		return video;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getGameVideoList(Game game) throws Exception {
		List<Video> video;
		String query = "SELECT Video FROM Game AS game RIGHT JOIN game.refVideo AS video WHERE game.id = "
				+ game.getId() + " ORDER BY video.name";

		video = em.createQuery(query).getResultList();

		return video;
	}

	@Override
	public Video mergeVideo(Video video) throws Exception {
		Video retVideo = dao.merge(video);
		return retVideo;
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
